/**
 * 
 */
package com.ccrc.lang;

import java.util.concurrent.ConcurrentMap;

import org.apache.commons.lang3.BooleanUtils;
import org.springframework.cglib.beans.BeanCopier;

import com.google.common.collect.Maps;

/**
 * @author Jalen
 *
 */
public final class BeanCopiers {
	
	private static ConcurrentMap<String, BeanCopier> beanCopierMap = Maps.newConcurrentMap();

	public BeanCopiers() {
	
	}

	public static void copyProperties(Object source, Object target) {
		String beanKey = generatedKey(source.getClass(), target.getClass());
		
		BeanCopier copier = null;
		if(BooleanUtils.isFalse(beanCopierMap.containsKey(beanKey))){
			copier = BeanCopier.create(source.getClass(), target.getClass(), false);
			beanCopierMap.put(beanKey, copier);
		}else{
			copier = beanCopierMap.get(beanKey);
		}
		copier.copy(source, target, null);
	}

	private static String generatedKey(Class<? extends Object> class1, Class<? extends Object> class2) {
		return KeyUtil.getKey(class1.getSimpleName(), class2.getSimpleName());
	}


}
